import {
	onLoad,
	onPageScroll,
	onReachBottom
} from '@dcloudio/uni-app';
import {
	ref
} from "vue";
export function usePaging(pageMethod, pageForm, dataHandle) {
	var page = 1
	var limit = 8
	const list = ref([])
	var total = 0
	const finish = ref(false)
	const loadStatus = ref(false)

	const query = () => {
		page = 1
		list.value = []
		finish.value = false
		getList()
	}

	const getList = () => {
		pageMethod({
			pageNo: page,
			pageSize: limit,
			...pageForm
		}).then(res => {
			loadStatus.value = false
			if (dataHandle) {
				dataHandle(res.list)
			}
			list.value.push(...res.list)
			total = res.total
			if (list.value.length >= total || res.list.length == 0) {
				finish.value = true
			}
			// list.value = res
		})

	}

	const nextHandle = () => {
		page++
		getList()
	}

	onReachBottom(() => {
		if (loadStatus.value || finish.value) {
			return
		}
		nextHandle()
	})

	return {
		list,
		loadStatus,
		query
	}
}